<template>
  <div class="app-container">
    <div>
      <!-- 假期余额 -->
      <div style="display: flex; justify-content: space-between">
        <el-row style="margin-bottom: 20px">
          <el-col>
            <!-- <el-button
              type="primary"
              plain
              icon="Download"
              v-hasPermi="['vacation:balance:export']"
              @click="handleExport"
              >导出</el-button
            > -->
          </el-col>
        </el-row>
        <div class="query-right" style="margin-bottom: 20px">
          <el-select
            v-model="cardvalue"
            placeholder="全公司"
            style="width: 120px"
            @change="querySelect"
          >
            <el-option
              v-for="item in cardoptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>

          <select-show
            v-if="selectStatus"
            v-model:orgList="form.rangeList"
            type="org"
            :disable-select-children-dept="false"
            :multiple="true"
            :showStyle="3"
          ></select-show>
        </div>
      </div>

      <el-table
        v-loading="loading"
        :data="postList"
        :default-sort="{ prop: 'name', order: 'descending' }"
      >
        <el-table-column label="姓名" align="center" prop="name" width="150" />
        <el-table-column
          label="部门"
          align="center"
          prop="deptName"
          width="150"
        />
        <el-table-column
          label="入职时间"
          align="center"
          prop="hiredate"
          width="150"
        />
        <template v-if="vacationNameList.length != 0">
          <el-table-column
            :label="item.name"
            align="center"
            :prop="item.id"
            v-for="item in vacationNameList"
            :key="item.id"
          >
          </el-table-column>
          <el-table-column label="操作" align="center">
            <template #default="scope">
              <el-button
                link
                type="primary"
                icon="Edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['vacation:balance:edit']"
                >修改</el-button
              >
            </template>
          </el-table-column>
        </template>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <!-- 修改假期余额弹出框 -->
    <el-dialog title="修改假期余额" v-model="dialogVisible" width="410">
      <div>
        <el-form :model="ruleForm">
          <el-form-item label="姓名" prop="name" label-width="80">
            <el-input
              placeholder="请输入内容"
              v-model="editRow.name"
              :disabled="true"
              style="width: 200px"
            >
            </el-input>
          </el-form-item>

          <el-form-item
            :label="item.name"
            v-for="(item, i) in vacationNameList"
            label-width="80"
          >
            <div
              style="
                display: flex;
                justify-content: space-between;
                align-items: center;
              "
            >
              <el-input
                placeholder="请输入余额"
                v-model="vacationNum[i]"
                type="number"
                style="width: 200px"
              >
              </el-input>
              <el-button
                type="primary"
                plain
                style="margin-left: 10px"
                @click="handleConfirm(item.rowId, vacationNum[i])"
                >确认修改</el-button
              >
            </div>
          </el-form-item>
        </el-form>
      </div>

      <div
        slot="footer"
        class="dialog-footer"
        style="display: flex; justify-content: end"
      >
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import {
  vacationBalanceList,
  getListVacationNameApi,
  updataValanceApi,
} from "@/api/vacation/balance";
import { ref } from "vue";
import selectShow from "@/views/flyflow/components/orgselect/selectAndShow.vue";

const { proxy } = getCurrentInstance();
const postList = ref([]);
const loading = ref(true);
const cardvalue = ref("");
const total = ref(0);
const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    postCode: undefined,
    postName: undefined,
    status: undefined,
    deptIds: [],
    empIds: [],
    isDimission: 0,
  },
});
const form = ref({
  rangeList: [],
});
const { queryParams } = toRefs(data);
const cardoptions = [
  {
    value: "全公司",
    label: "全公司",
  },
  {
    value: "部门/人员",
    label: "部门/人员",
  },
  {
    value: "已离职人员",
    label: "已离职人员",
  },
];
const dialogVisible = ref(false);
const editRow = ref({});
const ruleForm = ref({});
const vacationNum = ref([]);

/**
 * 根据选择的部门、人员集合的变化去查询
 */
watch(
  () => form.value.rangeList,
  (value) => {
    value.map((item) => {
      if (item.type == "user") {
        queryParams.value.empIds.push(item.id);
      } else if (item.type == "dept") {
        queryParams.value.deptIds.push(item.id);
      }
    });
    getList();
  }
);

/** 查询岗位列表 */
function getList(e) {
  loading.value = true;
  vacationBalanceList(queryParams.value).then((response) => {
    response.rows.map((item) => {
      item.userBalanceList.map((child) => {
        item[child.ruleId] = child.num;
      });
    });

    postList.value = response.rows;
    total.value = Number(response.total);
    loading.value = false;
  });
}

/**
 * 查询所有的假期规则名称和id
 */
let vacationNameList = ref([]);
const getListVacationName = () => {
  getListVacationNameApi().then((res) => {
    vacationNameList.value = res.data;
  });
};

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "vacation/balance/export",
    {
      ...queryParams.value,
    },
    `balance_${new Date().getTime()}.xlsx`
  );
}

/**
 * 下拉框查询
 */
const selectStatus = ref(false);
const querySelect = (val) => {
  if (val === "已离职人员") {
    queryParams.value.isDimission = 1;
    queryParams.value.pageNum = 1;
    getList();
  } else if (val === "全公司") {
    queryParams.value.deptIds = [];
    queryParams.value.empIds = [];
    queryParams.value.pageNum = 1;
    getList();
  } else if (val === "部门/人员") {
    selectStatus.value = true;
  }
};

// 点开编辑 重构一些数据
const handleUpdate = (row) => {
  editRow.value.name = row.name;
  vacationNameList.value.map((item) => {
    row.userBalanceList.map((child) => {
      if (item.id == child.ruleId) {
        item.num = child.num;
        item.rowId = child.id;
      }
    });
  });

  vacationNameList.value.map((item) => {
    vacationNum.value.push(item.num ? Number(item.num) : 0);
  });
  dialogVisible.value = true;
};

/**
 * 点击确认修改
 * @param id
 * @param num
 */
const handleConfirm = (id, num) => {
  updataValanceApi({ id, num }).then((res) => {
    if (res.code == 200) {
      proxy.$modal.msgSuccess("修改成功");
    }
  });
};

const confirm = () => {
  getList();
  getListVacationName();
  dialogVisible.value = false;
};

getList();
getListVacationName();
</script>

<style lang="scss" scoped>
.selectStatusStyle {
  margin: 0 30px;
}

.content {
  width: 100%;
  display: flex;
  .content_left {
    width: 50%;
    padding: 15px;

    .content_box {
      width: 100%;
      display: flex;
      height: 35px;
      align-items: center;
      position: relative;
    }
  }
  .content_center {
    border-left: 1px solid #f6f6f6c7;
    height: 500px;
  }
  .content_right {
    width: 50%;
    padding: 15px;
  }
}

.query-right {
  display: flex;
}
</style>
